package com.tanhua.server.service;

import com.tanhua.commons.templates.HuanXinTemplate;
import com.tanhua.manage.domain.db.UserInfo;
import com.tanhua.manage.domain.mongo.Comment;
import com.tanhua.manage.domain.mongo.Friend;
import com.tanhua.manage.domain.vo.ContactVo;
import com.tanhua.manage.domain.vo.MessageVo;
import com.tanhua.manage.domain.vo.PageResult;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptor.UserHolder;

import com.tanhua.server.utils.RelativeDateFormat;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class IMService {

    @Reference
    private FriendApi friendApi;
    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @Reference
    private UserInfoApi userInfoApi;
    @Reference
    private CommentApi commentApi;

    /**
     * 聊一聊后的交友, 添加联系人
     * @param userId
     */
    public void makeFriends(Long userId) {

        friendApi.makeFriend(UserHolder.getUserId(),userId);
    }

    /**
     * 联系人列表
     * @param page
     * @param pageSize
     * @param keyword 暂时不理它
     * @return
     */
    public PageResult<ContactVo> queryContactsList(Long page, Long pageSize, String keyword) {
        //调用api查询分页结果
        PageResult pageResult = friendApi.findPage(UserHolder.getUserId(),page,page);
        //抽出friend集合
        List<Friend> friends = pageResult.getItems();
        //判断集合是否有结果
        if (!CollectionUtils.isEmpty(friends)){
            //获取朋友的id，通过id获取用户信息
            List<Long> friendIds = friends.stream().map(Friend::getFriendId).collect(Collectors.toList());

            List<UserInfo> userInfoList = userInfoApi.findByBatchId(friendIds);

            List<ContactVo> voList = userInfoList.stream().map(userInfo -> {
                ContactVo vo = new ContactVo();
                BeanUtils.copyProperties(userInfo, vo);
                vo.setUserId(userInfo.getId().toString());

                return vo;
            }).collect(Collectors.toList());
        pageResult.setItems(voList);
        }
        return pageResult;
    }

    /**
     * 谁对我点赞 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    public PageResult<MessageVo> messageCommentList(int commentType, Long page, Long pageSize) {
        //1.调用api，按commentType分页查询
        PageResult pageResult = commentApi.findPageByUserId(UserHolder.getUserId(),commentType,page,pageSize);
        //2.分页结果集
        List<Comment> commentList = pageResult.getItems();

        if (!CollectionUtils.isEmpty(commentList)) {
            //3.取出操作者的id集合
            List<Long> userIds = commentList.stream().map(Comment::getUserId).collect(Collectors.toList());
            //4.批量查询操作者的信息
            //转成map，key为id，value为用户信息
            Map<Long, UserInfo> userInfoMap = userInfoApi.findByBatchId(userIds).stream().collect(Collectors.toMap(UserInfo::getId, u -> u));
            //5.转vo
            List<MessageVo> voList = commentList.stream().map(comment -> {
                MessageVo vo = new MessageVo();
                UserInfo userInfo = userInfoMap.get(comment.getUserId());
                BeanUtils.copyProperties(userInfo, vo);
                vo.setId(userInfo.getId().toString());
                vo.setCreateDate(RelativeDateFormat.format(new Date(comment.getCreated())));
                return vo;
            }).collect(Collectors.toList());
            pageResult.setItems(voList);
        }
        return pageResult;
    }
}
